<?php
/*
+--------------------------------------------------------------------------
|   Anwsion [#RELEASE_VERSION#]
|   ========================================
|   by Tatfook Network Team
|   (c) 2011 - 2012 Anwsion Software
|   http://www.anwsion.com
|   ========================================
|   Support: zhengqiang@gmail.com
|   
+---------------------------------------------------------------------------
*/

class c_tool_class extends AWS_CONTROLLER
{

	function get_permission_action()
	{
	}

	public function setup()
	{
		admin_session_class::check_admin_login($this->get_permission_action());
		$this->crumb("系统维护", "admin/tool/");
	}

	/**
	 * 清除系统缓存
	 * Enter description here ...
	 */
	public function cache_clean_action()
	{
		$this->crumb("缓存管理", "admin/tool/cache_clean");
		
		TPL::output('admin/tool/cache_clean');
	}

	public function cache_clean_process_action()
	{
		$this->model('db_cache')->clean();
		
		ZCACHE::$cache_open = TRUE;
		
		ZCACHE::clean();
		
		ZCACHE::$cache_open = FALSE;
		
		$this->crumb("缓存管理", "admin/tool/cache_clean");
		
		TPL::assign('message', '成功清除网站全部缓存');
		TPL::output('admin/tool/cache_clean');
	}

	public function keyword_index_action()
	{
		$this->crumb("更新关键字索引", "admin/tool/keyword_index/");
		
		TPL::output('admin/tool/keyword_index');
	}

	public function keyword_index_process_action()
	{

		$pa = new Services_Phpanalysis_Phpanalysis('utf-8', 'utf-8', false);

		$this->crumb("更新关键字索引", "admin/tool/keyword_index/");
		
		if ($this->is_post())
		{
			$per_page = intval($_POST['per_page']);
			HTTP::redirect('/admin/tool/keyword_index_process/per_page-' . $per_page);
		}
		
		$per_page = intval($_GET['per_page']);
		$page = intval($_GET['page']);
		$interval = intval($_GET['interval']);
		
		$question_num = $this->model('question')->get_question_list(true);
		
		if ($question_list = $this->model('question')->get_question_list(false, '', $page * $per_page . ',' . $per_page)) //未处理完，继续处理
		{
			$pa = new Services_Phpanalysis_Phpanalysis('utf-8', 'utf-8', false);
			//载入词典
			$pa->LoadDict();
			
			foreach ($question_list as $key => $val)
			{
				//执行分词
				$question_content_tmp = clear_point(strtolower($val['question_content']));
				$pa->SetSource($question_content_tmp);
				$pa->StartAnalysis(true);
				$okresult = $pa->GetFinallyResult(',', false);
				$okresult_array = preg_split("/,/", $okresult);
				
				if (is_array($okresult_array))
				{
					$this->model('question')->delete_question_keyword($val['question_id']);
					
					$okresult_array = array_unique($okresult_array);
					
					foreach ($okresult_array as $py_key => $py_val)
					{
						if ($py_val)
						{
							if (cjk_strlen($py_val) > 1)
							{
								$this->model('question')->add_question_keyword($val['question_id'], $py_val);
							}
						}
					}
				
				}
			}
			
			++ $page;
			
			$current = ($page * $per_page) > $question_num ? $question_num : ($page * $per_page);
			
			$interval = ($interval == 0) ? 3 : $interval;
			
			$url = '/admin/tool/keyword_index_process/page-' . $page . '__per_page-' . $per_page . '__interval-' . $interval;
			H::redirect_msg('总共 ' . $question_num . ' 个问题，当前更新到第 ' . $current . ' 个问题', $url, $interval);
		}
		else //提示处理完成
		{
			TPL::assign('msg', '关键词索引更新完成，总共更新 ' . $question_num . ' 个问题的关键词索引。');
			TPL::output('admin/tool/keyword_index');
		}
	}

	public function database_action()
	{
		$this->crumb("数据库维护", "admin/tool/database/");
		
		TPL::assign('backup_dir', $this->model('db_manage')->get_backup_dir());
		TPL::assign('list', $this->model('db_manage')->get_file_list());
		TPL::output('admin/tool/database');
	}

	public function database_backup_process_action()
	{
		if ($this->is_post())
		{
			foreach ($_POST as $key => $val)
			{
				$gets[] = "$key-$val";
			}
			
			header('Location: ' . get_js_url('?/admin/tool/database_backup_process/' . implode('__', $gets)));
			exit();
		}
		
		$sqlstr = '';
		$size = intval($_GET['size']);
		$page = intval($_GET['page']);
		$tid = intval($_GET['tid']);
		$start = intval($_GET['start']);
		
		$tables = $this->model('db_manage')->get_table_list();
		
		if (! $tid && $page == 0)
		{
			foreach ($tables as $table)
			{
				$sqlstr .= $this->model('db_manage')->dump_struct($table);
			}
		}
		
		for ($finish = true; ($tid < count($tables)) && (strlen($sqlstr) + 300 < $size * 1024) && $finish; $tid ++)
		{
			$dump_query = $this->model('db_manage')->dump_record($tables[$tid], $size, $start, $finish, strlen($sqlstr));
			
			$sqlstr .= $dump_query['sqlstr'];
			$start = $dump_query['start'];
			$finish = $dump_query['finish'];
			
			if ($finish)
			{
				$start = 0;
			}
		}
		
		! $finish && $tid --;
		
		if (trim($sqlstr))
		{
			$page ++;
			
			$retval = $this->model('db_manage')->save_sql_file($_GET['filename'], $sqlstr, $page);
			
			if ($retval)
			{
				H::redirect_msg('正在备份，成功导出分卷 ' . $page . '...', '/admin/tool/database_backup_process/size-' . rawurlencode($size) . '__page-' . rawurlencode($page) . '__tid-' . rawurlencode($tid) . '__start-' . rawurlencode($start) . '__filename-' . rawurlencode($_GET['filename']));
			}
			else
			{
				die('无法保存文件，请检查 system 目录是否有写入权限');
			}
		}
		else
		{
			//提示备份完成，显示文件列表
			$this->crumb("数据库备份", "admin/tool/database/");
			
			for ($i = 1; $i <= $page; $i ++)
			{
				$backup_list[] = '/system/backup_' . get_setting('backup_dir') . '/' . $_GET['filename'] . '-' . $i . '.sql';
			}
			
			TPL::assign('list', $backup_list);
			TPL::output('admin/tool/database_backup_success');
		}
	}

	public function database_revert_process_action()
	{
		$filename = $_GET['filename'];
		
		$page = intval($_GET['page']);
		
		@set_time_limit(0);
		
		if (! $filename)
		{
			HTTP::redirect('/admin/tool/database/');
		}
		
		$file_list = $this->model('db_manage')->get_file_list();
		
		$page = ! $page ? 1 : $page;
		
		if ($page <= $file_list[$filename]['page'])
		{
			$retval = $this->model('db_manage')->revert($filename, $page);
			
			H::redirect_msg('正在还原，成功导入分卷 ' . $page . '...', '/admin/tool/database_revert_process/filename-' . rawurlencode($_GET['filename']) . '__page-' . rawurlencode(++ $page));
		}
		else
		{
			H::redirect_msg('成功导入 ' . count($file_list) . ' 个分卷，全部备份文件还原成功。');
		}
	}

	public function reputation_rebuild_action()
	{
		$this->crumb("更新威望数据", "admin/tool/reputation_rebuild/");
		
		TPL::output('admin/tool/reputation_rebuild');
	}

	public function rebuild_reputation_process_action()
	{
		$this->crumb("更新威望数据", "admin/tool/reputation_rebuild/");
		
		if ($this->is_post())
		{
			$per_page = intval($_POST['per_page']);
			HTTP::redirect('/admin/tool/rebuild_reputation_process/per_page-' . $per_page);
		}
		
		$per_page = intval($_GET['per_page']);
		$page = intval($_GET['page']);
		$interval = intval($_GET['interval']);
		
		$user_count = $this->model('account')->get_user_count();
		
		if ($page * $per_page < $user_count) //未处理完，继续处理
		{
			$this->model('reputation')->cron(($page ++) * $per_page, $per_page);
			
			$current = ($page * $per_page) > $user_count ? $user_count : ($page * $per_page);
			
			$interval = ($interval == 0) ? 3 : $interval;
			
			$url = '/admin/tool/rebuild_reputation_process/page-' . $page . '__per_page-' . $per_page . '__interval-' . $interval;
			
			H::redirect_msg('总共 ' . $user_count . ' 个用户，当前更新到第 ' . $current . ' 个用户', $url, $interval);
		}
		else //提示处理完成
		{
			TPL::assign('msg', '威望数据更新完成，总共更新 ' . $user_count . ' 个用户的威望数据。');
			TPL::output('admin/tool/reputation_rebuild');
		}
	}

}